package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;


import po.Employment;
import po.Rating;
import po.User;
import service.SearchService;
import service.UserService;

public class SearchAction extends ActionSupport{
	private Employment emp;
	private User user;
	private List<Employment> list;
	int empID;
	
	public void setEmpID(int empID) {
		this.empID = empID;
	}
	
	public void setEmp(Employment emp) {
		this.emp = emp; 
	}
	public Employment getEmp() {
		return this.emp;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public User getUser() {
		return this.user;
	}
	public void setList(List list) {
		this.list = list;
	}
	public List getList() {
		return this.list;
	}
	
	//进入查看招聘信息界面时，先加载一整页的信息
	public String getAll() {
		SearchService ss = new SearchService();
		if(ss.getAllEmployment()) {
			list = ss.getList();
			
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		
		//得到用户id 正常登陆取值正常
		HttpSession session = request.getSession();
		int userID = (int) session.getAttribute("userID");
		UserService us = new UserService();
		if(us.isTutor(userID)) {
			return "success";
		}
		request.setAttribute("tip", "请先提交相应材料注册家教！");
		return "noright"; 
	}
	
	//由输入信息筛选招聘信息
	public String search() {
		SearchService ss = new SearchService();
		if(ss.searchEmployment(emp)) {
			list = ss.getList();
			return "success";
		}
		return "fail";
	}
	
	public void getUserInfo() {
//		int empID = 0;
		HttpServletRequest request = ServletActionContext.getRequest(); //得到quest对象
		HttpServletResponse response = ServletActionContext.getResponse();
//		if(request.getAttribute("empID") != null) {
//			empID = (int)request.getAttribute("empID");
//		}
		System.out.println("传入的empID="+empID);
		SearchService ss = new SearchService();
		user = ss.searchUserByEmpID(empID);
		Employment emp = ss.searchEmpByEmpID(empID);
		
		PrintWriter out;
		
		try {
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			
			out.println("账户名："+user.getAccount()+"<br>");
			out.println("联系方式："+user.getUserDetail().getPhone()+"<br>");	
			out.println("电子邮箱："+user.getUserDetail().getEmail()+"<br>");	
			out.println("具体课程要求："+"<br>"+emp.getNeedOrPresentation()+"<br>");
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}						
	}
	
	//由传入的订单id查找用户的申请信息
	private int orderID;
	public void setOrderID(int orderID) {
		this.orderID = orderID;
	}
	public void getApplyInfo() {
		HttpServletResponse response = ServletActionContext.getResponse();
		SearchService ss = new SearchService();
		User user = ss.searchApplyInfo(orderID);
		PrintWriter out;
		
		try {
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();		
			out.println("账户名："+user.getAccount()+"<br>");
			out.println("联系方式："+user.getUserDetail().getPhone()+"<br>");	
			out.println("电子邮箱："+user.getUserDetail().getEmail()+"<br>");	
			out.println("备注信息："+"<br>");
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	//用户查看家教的信息-要完善评价和评分
	public void getTutorInfo() {
		HttpServletResponse response = ServletActionContext.getResponse();
		SearchService ss = new SearchService();
		User user = ss.searchIssueInfo(orderID);
		Rating rat = user.getRat();
		PrintWriter out;
		
		try {
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();		
			out.println("账户名："+user.getAccount()+"<br>");
			out.println("联系方式："+user.getUserDetail().getPhone()+"<br>");	
			out.println("电子邮箱："+user.getUserDetail().getEmail()+"<br>");
			//完善评价系统后修改
			out.println("完成课程数："+rat.getNumber()+"<br>");
			out.println("用户评价："+"<span style=\"color:green\">"+"好评数："+rat.getGoodMark()+"  </span>"+"<span style=\"color:red\">"+"  差评数:"+rat.getBadMark()+"</span>"+"<br>");
			out.println("综合评分:"+rat.getAvgMark()+"<br>");
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
}
